package com.mingtu.dzpatrol.activity;

import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.StringUtils;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import com.hjq.gson.factory.GsonFactory;

import com.mingtu.dzpatrol.R;
import com.mingtu.dzpatrol.base.BaseActivity;
import com.mingtu.dzpatrol.bean.CameraTypeBean;
import com.mingtu.dzpatrol.callback.StringDialogCallback;
import com.mingtu.dzpatrol.fragment.MonitoringDequiFragment;
import com.mingtu.dzpatrol.utils.Constant;
import com.mingtu.dzpatrol.viewpager.LazyViewPager.LazyFragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MonitoringDequipmentActivity extends BaseActivity {


    @BindView(R.id.slide_tab)
    SlidingTabLayout slideTab;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private String[] mTitles;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_monitoring_dequipment;
    }

    @Override
    protected void initView() {
        setModuleTitle("监控设备管理");
    }

    @Override
    protected void initData() {
        MyPagerAdapter mAdapter = new MyPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(mAdapter);
    }

    @Override
    protected void getData() {
        getCameraType();
    }

    private class MyPagerAdapter extends LazyFragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        protected Fragment getItem(ViewGroup viewGroup, int position) {
            return mFragments.get(position);
        }
    }

    /**
     * 摄像头类型
     */
    private void getCameraType() {
        OkGo.<String>post(Constant.getInstance().POST_CAMERA_TYPE)
                .tag(this)
                .upJson("")
                .execute(new StringDialogCallback(this) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        if (!StringUtils.isEmpty(result)) {
                            try {
                                Gson gson = GsonFactory.getSingletonGson();
                                CameraTypeBean cameraTypeBean = new CameraTypeBean();
                                cameraTypeBean = gson.fromJson(result, CameraTypeBean.class);

                                List<String> typeList = cameraTypeBean.getData();
                                if (typeList != null) {
                                    int size = typeList.size();
                                    if (size > 0) {
                                        mTitles = new String[size];
                                        for (int i = 0; i < size; i++) {
                                            String typeName = typeList.get(i);
                                            mTitles[i] = typeName;
                                            mFragments.add(MonitoringDequiFragment.getInstance(typeName));
                                        }
                                        slideTab.setViewPager(viewPager, mTitles, MonitoringDequipmentActivity.this, mFragments);
                                        slideTab.setOnTabSelectListener(new OnTabSelectListener() {
                                            @Override
                                            public void onTabSelect(int position) {
                                                viewPager.setCurrentItem(position);
                                            }

                                            @Override
                                            public void onTabReselect(int position) {

                                            }
                                        });

                                    }
                                }

                            } catch (JsonSyntaxException e) {
                                e.printStackTrace();
                            }

                        }
                    }
                });
    }

}